home *** CD-ROM | disk | FTP | other *** search
- Path: qualcomm.com!not-for-mail
- From: drew@qualcomm.com (Drew Eckhardt)
- Newsgroups: comp.lang.c++
- Subject: Re: INITIALIZATION of a MEMBER CLASS of a CLASS
- Date: 17 Apr 1996 14:19:18 -0600
- Organization: QUALCOMM, Incorporated; Boulder, CO, USA;
- Message-ID: <4l3jo6$jpl@qualcomm.com>
- References: <4l37o3$iug@risky.ecs.umass.edu>
- NNTP-Posting-Host: littlebear.qualcomm.com
-
- In article <4l37o3$iug@risky.ecs.umass.edu>,
- Kiran K Toutireddy <ktoutire@chopin.ecs.umass.edu> wrote:
- >
- >
- >Hi,
- > I have a question related to initializing the member classes inside a class data structure. The following example should explain the question.
-
-
-
- > I want to initialize the car_engine member to be Gas engine by default, instead of the default electric engine. How do I do that?
-
- Initializer lists.
-
- > the following is some pseudo code of the above example
- >
- > class Engine{
- > nuts..bolts..pipes..rods...;
- > public:
- > Engine(){ make this an electric engine,....;}
- > Engine(int Gas){ make it a gas engine..}
- > Engine(float Deisel){ make it a deisel engine;}
- > };
- >
- > class Car{
- > Engine car_engine;
- > public:
- Car::Car() :
- Engine (/* Some integer here */)
- {
- // Body of constructor here.
- }
- > };
- >
-
- A few other related comments:
-
- 1. If you wish to specify multiple initializers, you use a comma
- delimited list. Ie
-
- foo::foo () :
- member1(x),
- member2(member1)
-
- 2. Base classes with parameterized constructors can also be initialized
- this way; although you use the class name instead of the member name.
-
- 3. Initializers are executed in the order of member declaration,
- not initializer ordering. Ie, given
-
- struct foo
- {
- public:
- foo (int a, int b) : x (a), y(b) {}
- private:
- int y, x;
- }
-
- y would be initialized first.
-
- >I tried initializing the car_engine by saying Engine car_engine(int Gas), but it gives a warning saying that ANSI c++ forbids such declarations.
-
- That would declare a function called car_engine taking an integer parameter
- and returning an Engine.
-
-
- --
- <a href="http://www.poohsticks.org/drew/">Home Page</a>
- Four boxes : soap, ballot, jury, ammo. | Work: drew@Qualcomm.COM
- Use in that order. | Play: drew@PoohSticks.ORG
-